﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using Microsoft.ApplicationBlocks.Data;


namespace AnyOffice.general.attend.personal
{
    public partial class register : ModulePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            int num = (Request.QueryString["no"] != null) ? Convert.ToInt32(Request.QueryString["no"].ToString()) : 0;
            int num2 = 30;
            int num3 = 10;
            int num4 = 10;
            int num5 = 60;
            AttendParaDao dao = new AttendParaDao();
            dao.GetPara();
            num2 = dao.DutyIntervalBefore1;
            num3 = dao.DutyIntervalAfter1;
            num4 = dao.DutyIntervalBefore2;
            num5 = dao.DutyIntervalAfter2;
            DataTable userConfig = new AttendConfigDao().GetUserConfig(base.Conn, HttpContext.Current.User.Identity.Name);
            if (userConfig.Rows.Count == 0)
            {
                base.CloseConn();
                Response.Redirect("duty.aspx");
            }
            if (userConfig.Rows[0]["DutyTime" + num.ToString()] != DBNull.Value)
            {
                DateTime time3;
                DateTime time4;
                int num6 = Convert.ToInt32(userConfig.Rows[0]["DutyType" + num].ToString());
                DateTime time2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime" + num].ToString());
                if (num6 == 1)
                {
                    time3 = time2.AddMinutes((double)-num2);
                    time4 = time2.AddMinutes((double)num3);
                }
                else
                {
                    time3 = time2.AddMinutes((double)-num4);
                    time4 = time2.AddMinutes((double)num5);
                }
                string commandText = "SELECT * FROM [dbo].[Attend_Duty] WHERE [UserId] = '" + base.User.Identity.Name + "' AND [RegisterTime] >='" + DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00' AND [RegisterTime] <= '" + DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59'";
                DataTable table2 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , commandText).Tables[0];
                if (((table2.Select(string.Concat(new object[] { "RegisterType = ", num6.ToString(), " AND RegisterTime >='", time3.ToString(), "' AND RegisterTime <='", time4, "'" })).Length == 0) && (time3 <= DateTime.Now)) && (DateTime.Now <= time4))
                {
                    AttendDutyData data = new AttendDutyData();
                    data.UserId = base.User.Identity.Name;
                    data.RegisterType = num6;
                    data.RegisterTime = DateTime.Now;
                    data.RegisterIp = Request.UserHostAddress;
                    if ((num6 == 1) && (DateTime.Now > time2))
                    {
                        data.RegisterFlag = 1;
                    }
                    else if ((num6 == 2) && (DateTime.Now < time2))
                    {
                        data.RegisterFlag = 2;
                    }
                    new AttendDutyDao().Insert(base.Conn, data);
                }
                base.CloseConn();
                Response.Redirect("duty.aspx");
            }
        }
 

    }
}
